package com.java110.mall.acct.integral.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.integral.DeductionIntegralDto;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.integral.PreStoreUserIntegralDto;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.mall.acct.integral.IDeductionIntegral;
import com.java110.po.integral.IntegralDetailPo;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeductionIntegralImpl implements IDeductionIntegral {


    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;


    @Override
    public int deduction(String orderId) {

        String deductionIntegralDtoStr = CommonCache.getAndRemoveValue("integral_deduction_" + orderId);

        if (StringUtil.isEmpty(deductionIntegralDtoStr)) {
            return 0;

        }
        DeductionIntegralDto deductionIntegralDto = JSONObject.parseObject(deductionIntegralDtoStr, DeductionIntegralDto.class);
        if (deductionIntegralDto == null) {
            return 0;
        }


        IntegralDto integralDto = new IntegralDto();
        integralDto.setLink(deductionIntegralDto.getLink());
        integralDto.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);

        if (ListUtil.isNull(integralDtos)) {
            throw new IllegalArgumentException("积分账户不存在");
        }

        int amount = Integer.parseInt(integralDtos.get(0).getAmount());
        int integral = deductionIntegralDto.getIntegral();
        if (integral > amount) {
            throw new IllegalArgumentException("积分不够");
        }


        integralDto = new IntegralDto();
        integralDto.setObjId(deductionIntegralDto.getShopId());
        integralDto.setObjType(IntegralDto.OBJ_TYPE_STORE);
        List<IntegralDto> communityIntegrals = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);

        if (ListUtil.isNull(communityIntegrals)) {
            throw new IllegalArgumentException("商铺未开通积分账户");
        }

        //todo 业主账户扣款
        IntegralDetailPo integralDetailPo = new IntegralDetailPo();
        integralDetailPo.setIntegralId(integralDtos.get(0).getIntegralId());
        integralDetailPo.setAmount(integral + "");
        integralDetailPo.setRemark("业主缴费扣款");
        integralV1InnerServiceSMOImpl.withholdIntegral(integralDetailPo);


        //todo 小区账户 预存
        PreStoreUserIntegralDto preStoreUserIntegralDto = new PreStoreUserIntegralDto();
        preStoreUserIntegralDto.setIntegralId(communityIntegrals.get(0).getIntegralId());
        preStoreUserIntegralDto.setAmount(integral);
        preStoreUserIntegralDto.setOrderId(orderId);
        preStoreUserIntegralDto.setRemark(deductionIntegralDto.getRemark());
        integralV1InnerServiceSMOImpl.preStoreUserIntegral(preStoreUserIntegralDto);


        return 1;
    }
}
